package com.example.myapplication.dao;

import com.example.myapplication.model.Game;
import com.example.myapplication.model.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.QueryListener;

public class GameDao {

    // 单例模式
    private static GameDao instance;

    public static GameDao getInstance() {
        if (instance == null) {
            instance = new GameDao();
        }
        return instance;
    }

    public GameDao() {}

    /**
     * 创建新活动
     */
    public void createGame(Game game, final DataCallback<String> callback) {
        game.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, BmobException e) {
                if (e == null) {
                    callback.onSuccess(objectId);
                } else {
                    callback.onFailure("创建失败: " + e.getMessage());
                }
                callback.onComplete();
            }
        });
    }

    /**
     * 根据ID获取活动详情
     */
    public void getGameById(String id, final DataCallback<Game> callback) {
        BmobQuery<Game> query = new BmobQuery<>();
        query.include("publisher");             // 包含发布者完整信息
        query.getObject(id, new QueryListener<Game>() {
            @Override
            public void done(Game game, BmobException e) {
                if (e == null && game != null) {
                    callback.onSuccess(game);
                } else {
                    String error = e != null ? e.getMessage() : "活动不存在";
                    callback.onFailure("查询失败: " + error);
                }
                callback.onComplete();
            }
        });
    }

    /**
     * 更新活动信息
     */
    public void updateGame(Game game, final DataCallback<Void> callback) {
        game.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    callback.onSuccess(null);
                } else {
                    callback.onFailure("更新失败: " + e.getMessage());
                }
                callback.onComplete();
            }
        });
    }

    /**
     * 删除活动
     */
    public void deleteGame(String gameId, final DataCallback<Void> callback) {
        Game game = new Game();
        game.setObjectId(gameId);
        game.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    callback.onSuccess(null);
                } else {
                    callback.onFailure("删除失败: " + e.getMessage());
                }
                callback.onComplete();
            }
        });
    }

    /**
     * 分页查询活动列表
     */
    public void getGamesByPage(int page, int pageSize, String sportType,
                               final DataCallback<List<Game>> callback) {
        BmobQuery<Game> query = new BmobQuery<>();
        query.setLimit(pageSize);
        query.setSkip((page - 1) * pageSize);
        query.order("-createdAt");
        query.include("publisher");

        if (sportType != null && !sportType.isEmpty()) {
            query.addWhereEqualTo("sportType", sportType);
        }

        query.findObjects(new FindListener<Game>() {
            @Override
            public void done(List<Game> list, BmobException e) {
                if (e == null) {
                    callback.onSuccess(list);
                } else {
                    callback.onFailure("查询失败: " + e.getMessage());
                }
                callback.onComplete();
            }
        });
    }

    /**
     * 查询用户发布的活动
     */
    public void getGamesByPublisher(User publisher, int page, int pageSize,
                                    final DataCallback<List<Game>> callback) {
        BmobQuery<Game> query = new BmobQuery<>();
        query.addWhereEqualTo("publisher", new BmobPointer(publisher));
        query.setLimit(pageSize);
        query.setSkip((page - 1) * pageSize);
        query.order("-createdAt");
        query.include("publisher");

        query.findObjects(new FindListener<Game>() {
            @Override
            public void done(List<Game> list, BmobException e) {
                if (e == null) {
                    callback.onSuccess(list);
                } else {
                    callback.onFailure("查询失败: " + e.getMessage());
                }
                callback.onComplete();
            }
        });
    }
}